{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gy3KpnNx_Jl4"
      },
      "source": [
        "##### Copyright 2025 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "fKqk8LWo_TCU"
      },
      "outputs": [],
      "source": [
        "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sP8PQnz1QrcF"
      },
      "source": [
        "# Gemini API: Providing base cases"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bxGr_x3MRA0z"
      },
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/prompting/Providing_base_cases.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ysy--KfNRrCq"
      },
      "source": [
        "LLMs require specific instructions to provide the expected results. Because of this, it is vital to ensure that the model knows how it should behave when it lacks information or when it should not answer a given query and provide a default response instead."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "Ne-3gnXqR0hI"
      },
      "outputs": [],
      "source": [
        "%pip install -U -q \"google-generativeai>=0.7.2\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "EconMHePQHGw"
      },
      "outputs": [],
      "source": [
        "import google.generativeai as genai"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eomJzCa6lb90"
      },
      "source": [
        "## Configure your API key\n",
        "\n",
        "To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "v-JZzORUpVR2"
      },
      "outputs": [],
      "source": [
        "from google.colab import userdata\n",
        "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
        "\n",
        "genai.configure(api_key=GOOGLE_API_KEY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FMne_rcZmz1X"
      },
      "source": [
        "## Examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0pZ-S0f8_mbQ"
      },
      "source": [
        "Let's go ahead and define the model, as well as give the model a template for how it should answer the question."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "O_q8ak-BU9kJ"
      },
      "outputs": [],
      "source": [
        "instructions = \"\"\"\n",
        "You are an assistant that helps tourists around the world to plan their vacation. Your responsibilities are:\n",
        "1. Helping book the hotel.\n",
        "2. Recommending restaurants.\n",
        "3. Warning about potential dangers.\n",
        "\n",
        "If other request is asked return \"I cannot help with this request.\"\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "8oS9LnnXXedG"
      },
      "outputs": [],
      "source": [
        "model = genai.GenerativeModel(model_name='gemini-2.0-flash', system_instruction=instructions)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "1k6LyJQYm1KM"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "ON TOPIC: Here are some things to look out for when visiting the beaches in San Diego:\n",
            "\n",
            "* **Strong currents:**  San Diego has beautiful beaches, but the Pacific Ocean can have strong currents.  Always swim near a lifeguard and be aware of your surroundings.\n",
            "* **Sunburns:**  The sun is strong in San Diego, even on cloudy days.  Use sunscreen, wear a hat, and take breaks from the sun.\n",
            "* **Rip currents:** These powerful currents can quickly pull swimmers out to sea. If caught in a rip current, don't fight against it. Swim parallel to the shore until you're out of the current, then swim back to shore.\n",
            "* **Sea lions:**  You may encounter sea lions on the beach. They can be aggressive, so keep a safe distance.\n",
            "* **Jellyfish:**  Jellyfish are common in San Diego waters, especially in the summer. Wear protective footwear in the water and avoid touching jellyfish.\n",
            "\n",
            "**Enjoy your time on the beaches of San Diego!** \n",
            "\n",
            "OFF TOPIC: I cannot help with this request. \n",
            "\n"
          ]
        }
      ],
      "source": [
        "print(\"ON TOPIC:\", model.generate_content(\"What should I look out for when I'm going to the beaches in San Diego?\").text)\n",
        "print(\"OFF TOPIC:\", model.generate_content(\"What bowling places do you recommend in Moscow?\").text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BPUkURgrYgBr"
      },
      "source": [
        "Let's try another template."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "oz-kWAKQYiGi"
      },
      "outputs": [],
      "source": [
        "instructions = \"\"\"\n",
        "You are an assistant at a library. Your task is to recommend books to people, if they do not tell you the genre assume Horror.\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "id": "3o5CRjdRaZfU"
      },
      "outputs": [],
      "source": [
        "model = genai.GenerativeModel(model_name='gemini-2.0-flash', system_instruction=instructions)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "PoDDTXGFm21K"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "## Specified genre:\n\n",
            "Of course! I'd be happy to recommend some books with hard magic systems. \n",
            "\n",
            "Here are three recommendations:\n",
            "\n",
            "1. **Mistborn: The Final Empire** by Brandon Sanderson: This is a classic of the genre, with a meticulously crafted magic system based on \"Allomancy,\" where people ingest metals to gain different powers. It's a thrilling story with a complex world and memorable characters.\n",
            "\n",
            "2. **The Lies of Locke Lamora** by Scott Lynch: This fantasy novel features a fascinating magic system based on the \"The Gentleman Bastards,\" a group of con artists who use a mixture of wit, cunning, and magic to pull off elaborate heists. \n",
            "\n",
            "3. **Jonathan Strange & Mr Norrell** by Susanna Clarke: This book features a unique and detailed magic system based on the \"English magicians\" who use a combination of rituals, incantations, and magical objects to perform their spells. It's a slow-burn read, but ultimately a rewarding one with a richly imagined world.\n",
            "\n",
            "Enjoy your reading! \n",
            "\n",
            "## Not specified genre:\n\n",
            "Sure! Since you didn't specify a genre, I'll recommend two spine-chilling horror novels:\n",
            "\n",
            "1. **\"The Haunting of Hill House\" by Shirley Jackson:** This classic explores the psychological and supernatural terrors that haunt a group of paranormal investigators in a sprawling, sinister mansion. \n",
            "\n",
            "2. **\"The Ritual\" by Adam Nevill:** This atmospheric thriller follows a group of friends on a hiking trip in the Swedish wilderness that turns into a terrifying fight for survival against an ancient evil. \n",
            "\n",
            "Let me know if you'd like suggestions in another genre! \n",
            "\n"
          ]
        }
      ],
      "source": [
        "print(\"## Specified genre:\n\n\", model.generate_content(\"Could you recommend me 3 books with hard magic system?\").text, sep=\"\\n\")\n",
        "print(\"## Not specified genre:\n\n\", model.generate_content(\"Could you recommend me 2 books?\").text, sep=\"\\n\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8mzHkChy_5Ec"
      },
      "source": [
        "## Next steps\n",
        "\n",
        "Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own data, or try some of the other prompting techniques such as few-shot prompting."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "Providing_base_cases.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
